package com.example.heipictureserver.mangger.websocket;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.example.heipictureserver.mangger.auth.SpaceUserAuthManager;
import com.example.heipictureserver.mangger.auth.modal.SpaceUserPermissionConstant;
import com.example.heipictureserver.modal.domain.Picture;
import com.example.heipictureserver.modal.domain.Space;
import com.example.heipictureserver.modal.domain.User;
import com.example.heipictureserver.modal.enums.SpaceTypeEnum;
import com.example.heipictureserver.service.PictureService;
import com.example.heipictureserver.service.SpaceService;
import com.example.heipictureserver.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/*
  *@Description 类功能简要描述
  *@Author bwg
  *@Date 2025/2/13 20:38
*/
 @Component
 @Slf4j
 public class WsHandshakeInterceptor implements HandshakeInterceptor {

  @Resource
  private UserService userService;

  @Resource
  private PictureService pictureService;

  @Resource
  private SpaceService spaceService;

  @Resource
  private SpaceUserAuthManager spaceUserAuthManager;

  @Override
  public boolean beforeHandshake(@NotNull ServerHttpRequest request, @NotNull ServerHttpResponse response, @NotNull WebSocketHandler wsHandler, @NotNull Map<String, Object> attributes) {
   if (request instanceof ServletServerHttpRequest) {
    HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
    // 获取请求参数
    String pictureId = servletRequest.getParameter("pictureId");
    if (StrUtil.isBlank(pictureId)) {
     log.error("缺少图片参数，拒绝握手");
     return false;
    }
    User loginUser = userService.getLoginUser(servletRequest);
    if (ObjUtil.isEmpty(loginUser)) {
     log.error("用户未登录，拒绝握手");
     return false;
    }
    // 校验用户是否有该图片的权限
    Picture picture = pictureService.getById(pictureId);
    if (picture == null) {
     log.error("图片不存在，拒绝握手");
     return false;
    }
    String spaceId = picture.getSpaceId();
    Space space = null;
    if (spaceId != null) {
     space = spaceService.getById(spaceId);
     if (space == null) {
      log.error("空间不存在，拒绝握手");
      return false;
     }
     if (space.getSpaceType() != SpaceTypeEnum.TEAM.getValue()) {
      log.info("不是团队空间，拒绝握手");
      return false;
     }
    }
    List<String> permissionList = spaceUserAuthManager.getPermissionList(space, loginUser);
    if (!permissionList.contains(SpaceUserPermissionConstant.PICTURE_EDIT)) {
     log.error("没有图片编辑权限，拒绝握手");
     return false;
    }
    // 设置 attributes
    attributes.put("user", loginUser);
    attributes.put("userId", loginUser.getId());
    attributes.put("pictureId", pictureId); // 记得转换为 Long 类型
   }
   return true;
  }

  @Override
  public void afterHandshake(@NotNull ServerHttpRequest request, @NotNull ServerHttpResponse response, @NotNull WebSocketHandler wsHandler, Exception exception) {
  }
 }

